package com.doitedu.hbase.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

/**
 * @Date 2021/12/13
 * @Created by HANGGE
 * @Description TODO
 */
public class HbaseUtil {
    /**
     * 获取Hbase的客户端连接
     * @return
     * @throws Exception
     */
     public  static Connection  getConnection() throws Exception{
         Configuration conf = HBaseConfiguration.create();
         conf.set("hbase.zookeeper.quorum" , "linux01:2181,linux02:2181,linux03:2181");
         Connection conn = ConnectionFactory.createConnection(conf);
         return  conn ;
     }

    public  static Admin getAdmin(Connection conn) throws Exception{
        return  conn.getAdmin() ;
    }

    /**
     * 获取表对象
     * @param tableName
     * @param conn
     * @return
     * @throws Exception
     */
     public static Table  getTable(String tableName , Connection conn) throws Exception{
         Table table = conn.getTable(TableName.valueOf(tableName));
         return table ;
     }

    /**
     * 展示行数据
     * @param result
     */
     public  static  void show(Result result){
         if (result!=null){
             while (result.advance()){// 每行数据的单元格 由迭代器遍历   判断是否还有下一个单元格
                 // 获取当前单元格
                 Cell cell = result.current();
                 //      rk001infoage  }���       *
                 //数据有自己的结构封装   不好解析 提供了解析Cell的工具类
                 byte[] cloneRow = CellUtil.cloneRow(cell);
                 byte[] cloneFamily = CellUtil.cloneFamily(cell);
                 byte[] cloneQualifier = CellUtil.cloneQualifier(cell);
                 byte[] cloneValue = CellUtil.cloneValue(cell);

                 System.out.println(new String(cloneRow)+":"+
                         new String(cloneFamily)+":"+
                         new String(cloneQualifier)+":"+
                         new String(cloneValue)
                 );

              /*  byte[] rowArray = cell.getRowArray();
                System.out.println(new String(rowArray));*/

             }
         }

     }


}
